package com.edu.hre.db.sql.recruit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreCandidateWorkExperience;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.recruit.CanExperienceForm;
import com.edu.hre.struts.form.recruit.CanExperienceListForm;

public class CanExperienceDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "canExperienceDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<CanExperienceForm> getCanExperienceList(int limit, int offset, int canId){

		List <HreCandidateWorkExperience> listHre = baseService.getPojos("candidateId", Compare.EQUAL, canId, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreCandidateWorkExperience.class);
		ArrayList<CanExperienceForm> listResult = new ArrayList<CanExperienceForm>();

		if (listHre == null) {
			return listResult;
		}
		for(int i = 0; listHre != null && i < listHre.size(); i++){

			try {
				CanExperienceForm experience = new CanExperienceForm();
				
				experience.setId(listHre.get(i).getId().intValue());
				experience.setCanId(canId);
				experience.setCompany(CommonUtils.normalizeString(listHre.get(i).getEmployer()));
				experience.setJobTitle(CommonUtils.normalizeString(listHre.get(i).getJobtit()));
				
				try {
					experience.setFromDate(CommonUtils.dateToString(listHre.get(i).getFromDate()));
					experience.setToDate(CommonUtils.dateToString(listHre.get(i).getToDate()));
				} catch (Exception e) {
					CommonUtils.println(e.getMessage());
				}

				experience.setComment(CommonUtils.normalizeString(listHre.get(i).getComments()));

				listResult.add(experience);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return listResult;
	}

	public int countCanExperiences(int canNumber){

		int count = baseService.countByColumns(new String[]{"candidateId"}, new int[]{Compare.EQUAL}, new Integer[]{canNumber}, "id", HreCandidateWorkExperience.class);
		return count < 0 ? 0 : count;
	}

	public void deleteExperiences(CanExperienceListForm form) {

		ArrayList<CanExperienceForm> info = (ArrayList<CanExperienceForm>) form.getAllExperiences();

		for (int i = 0; i < info.size(); i++) {

			try {

				CanExperienceForm work = info.get(i);
				if (work.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreCandidateWorkExperience.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
